package net.mcreator.toolsasfood.procedures;

import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.event.TickEvent;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.Entity;
import net.minecraft.sounds.SoundSource;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.BlockPos;

import net.mcreator.toolsasfood.network.ToolsAsFoodModVariables;

import javax.annotation.Nullable;

@Mod.EventBusSubscriber
public class TickProcedure {
	@SubscribeEvent
	public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			Entity entity = event.player;
			execute(event, entity.level, entity.getX(), entity.getY(), entity.getZ(), entity);
		}
	}

	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		execute(null, world, x, y, z, entity);
	}

	private static void execute(@Nullable Event event, LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		if (entity.getPersistentData().getBoolean("isEat")) {
			{
				double _setval = (entity.getCapability(ToolsAsFoodModVariables.PLAYER_VARIABLES_CAPABILITY, null)
						.orElse(new ToolsAsFoodModVariables.PlayerVariables())).eat_time + 1;
				entity.getCapability(ToolsAsFoodModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.eat_time = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
		} else {
			{
				double _setval = 0;
				entity.getCapability(ToolsAsFoodModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.eat_time = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
		}
		if ((entity.getCapability(ToolsAsFoodModVariables.PLAYER_VARIABLES_CAPABILITY, null)
				.orElse(new ToolsAsFoodModVariables.PlayerVariables())).eat_time % 4 == 1) {
			if (world instanceof Level _level) {
				if (!_level.isClientSide()) {
					_level.playSound(null, new BlockPos((int) x, (int) y, (int) z),
							ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.generic.eat")), SoundSource.MASTER, 1, 1);
				} else {
					_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.generic.eat")),
							SoundSource.MASTER, 1, 1, false);
				}
			}
		}
		if ((entity.getCapability(ToolsAsFoodModVariables.PLAYER_VARIABLES_CAPABILITY, null)
				.orElse(new ToolsAsFoodModVariables.PlayerVariables())).eat_time == 25) {
			if (world instanceof Level _level) {
				if (!_level.isClientSide()) {
					_level.playSound(null, new BlockPos((int) x, (int) y, (int) z),
							ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.player.burp")), SoundSource.MASTER, 1, 1);
				} else {
					_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.player.burp")),
							SoundSource.MASTER, 1, 1, false);
				}
			}
			entity.getPersistentData().putBoolean("isEat", (false));
			EatingProcedure.execute(world, x, y, z, entity);
		}
	}
}
